package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.bean.Dish;
import com.itheima.commons.R;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.channels.Pipe;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @date 2023/1/9
 */
@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品包含口味信息
     *
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dishDto) {
        //1.调用service新增方法
        dishService.addDishWithFlavor(dishDto);
        //2.返回结果
        return R.success("新增成功！");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int pageSize,
                                 String name){
        Page<Dish> dishPage = new Page<>(page,pageSize);
        //1.2构造条件查询，添加排序条件，根据sort字段进行排序，name名称进行分页查询
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Dish::getSort).like(name!=null,Dish::getName,name);
        dishService.page(dishPage,wrapper);
        //1.new一个Page<DishDto>对象
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        //2.将Page<Dish>对象除了records属性，全部拷贝Page<DishDto>
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        //3.根据分类id查询分类名称.需要查询Category表
        List<Dish> dishList = dishPage.getRecords();

        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : dishList) {
            Category category = categoryService.getById(dish.getCategoryId());
            DishDto dishDto = new DishDto();
            //4.List<DishDto>需要设置分类名称以及Dish所有属性
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setCategoryName(category!=null?category.getName():"未知类型");
            dishDtoList.add(dishDto);
        }
        //5.将List<DishDto>设置回Page<DishDto>
        dishDtoPage.setRecords(dishDtoList);
        //6.返回Page<DishDio>结果

        //优化
        return R.success(dishDtoPage);
    }


    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> list(@PathVariable Long id) {
        //1.调用service的查询方法
        DishDto dishDto = dishService.getDishWithFlavor(id);
        //2.直接返回查询结果
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        //1.调用service的更新方法
        dishService.updateWithFlavor(dishDto);
        //2.返回成功结果
        return R.success("修改成功！");
    }



    /**
     * 查询菜品列表数据包含口味信息
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> listWithFlavors(Dish dish){
        //1.调用service的查询列表方法
        List<DishDto> dishDtos = dishService.listWithFlavors(dish);
        //2.返回结果
        return R.success(dishDtos);
    }



    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam Long[] ids){
        //1.调用service的删除方法
        dishService.deleteWithFlavor(ids);
        //2.返回结果
        return R.success("删除成功！");
    }

    /**
     * 停售|起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> statusupdate(@PathVariable("status")Integer status,
                                  @RequestParam List<Long> ids){
        return dishService.sellStatus(status,ids);
    }
}
